package com.rm.zeyou.voice;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.MiddleNotify;
import com.rm.zeyou.entity.MiddleRelation;
import com.rm.zeyou.mapper.MiddleNotifyMapper;
import com.rm.zeyou.mapper.MiddleRelationMapper;
import com.rm.zeyou.mapper.VoiceNotifyMapper;
import com.rm.zeyou.mapper.VoiceTemplateConfigMapper;
import com.rm.zeyou.voice.vo.request.MiddleNumberAXBVo;
import com.rm.zeyou.voice.vo.request.MiddleNumberUnbindVo;
import com.rm.zeyou.voice.vo.request.VoiceNotifyVo;
import com.winnerlook.model.PrivacyBindBodyAxb;
import com.winnerlook.model.PrivacyUnbindBody;
import com.winnerlook.model.VoiceNotifyBody;
import com.winnerlook.model.VoiceResponseResult;
import com.winnerlook.service.VoiceSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * @author 🤙🤙🏼🤙🏽
 * @date 2025 4月 08 09:39
 * @Description
 */
@Component
@Slf4j
public class VoiceUtil {
    @Resource
    private MiddleRelationMapper middleRelationMapper;
    @Resource
    private VoiceNotifyMapper voiceNotifyMapper;
    @Resource
    private VoiceTemplateConfigMapper voiceTemplateConfigMapper;
    @Resource
    private MiddleNotifyMapper middleNotifyMapper;

    private final String ip = "https://www.zeyouinlife.com";
    private final String middleNumberAXBNotify = "/api/voiceNotify/middleNumberAXBNotify";
    private final String voiceNumberAXBNotify = "/api/voiceNotify/voiceNotify";
    private final String successCode = "000000";

    /**
     * 隐私小号AXB绑定
     *
     * @param bindVo
     * @return
     * @throws Exception
     * @Date 2025 4月 08 11:48
     */
    public Integer middleNumberAxb(MiddleNumberAXBVo bindVo) throws Exception {
        String middleNumber = voiceTemplateConfigMapper.selectOneData("middleNumber");
        String accountId = voiceTemplateConfigMapper.selectOneData("accountId");
        String token = voiceTemplateConfigMapper.selectOneData("token");
        Integer result = 0;
        PrivacyBindBodyAxb privacyBindBodyAxb = new PrivacyBindBodyAxb();
        privacyBindBodyAxb.setBindNumberA(bindVo.getBindNumberA());
        privacyBindBodyAxb.setBindNumberB(bindVo.getBindNumberB());
        privacyBindBodyAxb.setMiddleNumber(middleNumber);
        privacyBindBodyAxb.setCallbackUrl(ip + middleNumberAXBNotify);
        VoiceResponseResult voiceResponseResult = VoiceSender.httpPrivacyBindAxb(privacyBindBodyAxb, accountId, token);
        log.info("小号绑定结果:{}",JSON.toJSONString(voiceResponseResult));
        if (voiceResponseResult.getResult().equals(successCode)) {
            MiddleNotify middleNotify = new MiddleNotify();
            middleNotify.setOrderNo(bindVo.getOrderNo());
            middleNotify.setBindId(voiceResponseResult.getBindId());
            middleNotify.setUserId(bindVo.getUserId());
            middleNotify.setTid(bindVo.getTid());
            middleNotify.setDisplayNumber(voiceResponseResult.getMiddleNumber());
            middleNotifyMapper.insert(middleNotify);

            MiddleRelation insertMiddleRelation = new MiddleRelation();
            insertMiddleRelation.setUserId(bindVo.getUserId());
            insertMiddleRelation.setTid(bindVo.getTid());
            insertMiddleRelation.setBindId(voiceResponseResult.getBindId());
            insertMiddleRelation.setMiddleNumber(voiceResponseResult.getMiddleNumber());
            insertMiddleRelation.setBindNumberA(bindVo.getBindNumberA());
            insertMiddleRelation.setBindNumberB(bindVo.getBindNumberB());
            insertMiddleRelation.setBindStatus(NumberConstants.ONE_CONSTANTS);
            result = middleRelationMapper.insert(insertMiddleRelation);
        }

        return result;
    }

    /**
     * 隐私小号AXB解绑
     *
     * @param unBindVo
     * @return
     * @throws Exception
     * @Date 2025 4月 08 11:47
     */
    public Integer middleNumberUnbind(MiddleNumberUnbindVo unBindVo) throws Exception {
        String middleNumber = voiceTemplateConfigMapper.selectOneData("middleNumber");
        String accountId = voiceTemplateConfigMapper.selectOneData("accountId");
        String token = voiceTemplateConfigMapper.selectOneData("token");
        int result = 0;
        PrivacyUnbindBody unbindBody = new PrivacyUnbindBody();
        unbindBody.setBindNumberA(unBindVo.getBindNumberA());
        unbindBody.setBindNumberB(unBindVo.getBindNumberB());
        unbindBody.setMiddleNumber(middleNumber);
        unbindBody.setMode(unBindVo.getMode());
        VoiceResponseResult voiceResponseResult = VoiceSender.httpPrivacyUnbind(unbindBody, accountId, token);
        log.info("小号解绑结果:{}",JSON.toJSONString(voiceResponseResult));
        if (voiceResponseResult.getResult().equals(successCode)) {
            MiddleRelation middleRelation = middleRelationMapper.selectOne(new LambdaQueryWrapper<MiddleRelation>()
                    .eq(MiddleRelation::getBindNumberA, unBindVo.getBindNumberA()));
            result = middleRelationMapper.deleteMidRelation(middleRelation.getId());
        }
        return result;
    }

    /**
     * 语音通知
     *
     * @param voiceNotifyVo
     * @return
     * @throws Exception
     * @Date 2025 4月 08 14:46
     */
    public Integer voiceNotify(VoiceNotifyVo voiceNotifyVo) throws Exception {
        String voiceNumber = voiceTemplateConfigMapper.selectOneData("voiceNumber");
        String accountId = voiceTemplateConfigMapper.selectOneData("accountId");
        String token = voiceTemplateConfigMapper.selectOneData("token");
        VoiceNotifyBody voiceNotifyBody = new VoiceNotifyBody();
        voiceNotifyBody.setDisplayNumber(voiceNumber);
        voiceNotifyBody.setCalleeNumber(voiceNotifyVo.getCalleeNumber());
        voiceNotifyBody.setTemplateId(voiceNotifyVo.getTemplateId());
        voiceNotifyBody.setTemplateArgs(new LinkedHashMap<>());
        //voiceNotifyBody.setCallbackUrl(ip + voiceNumberAXBNotify);
        log.info("语音通知入参：{}", JSON.toJSONString(voiceNotifyBody));
        VoiceResponseResult voiceResponseResult = VoiceSender.httpSendVoiceNotify(voiceNotifyBody, accountId, token);
        log.info("语音通知结果返回：{}", JSON.toJSONString(voiceResponseResult));
        //if (voiceResponseResult.getResult().equals(successCode)) {
        //    VoiceNotify voiceNotify = new VoiceNotify();
        //    voiceNotify.setTid(voiceNotifyVo.getTid());
        //    voiceNotify.setOrderNo(voiceNotifyVo.getOrderNo());
        //    voiceNotify.setCallId(voiceResponseResult.getCallId());
        //    voiceNotify.setDisplayNumber(voiceNumber);
        //    voiceNotify.setCalleeNumber(voiceNotifyVo.getCalleeNumber());
        //    voiceNotify.setTemplateId(voiceNotifyVo.getTemplateId());
        //    voiceNotifyMapper.insert(voiceNotify);
        //}
        return 1;

    }
}
